import LoginForm from "../../component/LoginForm/index.tsx";
import { useConfig } from "../../hook/D8dConfigProvider.tsx";
import { navigate, setLocalStorage } from "../../utils/fun.ts";
import type { FormItemConfigs } from "../../global.ts";

interface LoginFormPageProps {
  /* 路由加载器 默认是 admin */
  loaderPath?: string;
}

export default function LoginFormPage({ loaderPath }: LoginFormPageProps) {
  const { setToken, setUserInfo, apiUrl, tokenKey,setUserMenu } = useConfig();
  const loginApiUrl = apiUrl?.login;
  const loginFormConfig: FormItemConfigs[] = [
    // {
    //   label:'登录角色',
    //   name:'登录角色',
    //   type:'select',
    //   options:[
    //     { label: '管理员', value: 'staff' },
    //     { label: '商户', value: 'merchant' },
    //   ],
    //   rules:[{ required: true, message: '请选择登录角色'}],
    // },
    {
      label: "用户名",
      name: "username",
      type: "input",
      initialValue: "admin",
      rules: [{ required: true, message: "请填写用户名" }],
    },
    {
      label: "密码",
      name: "password",
      type: "password",
      initialValue: "123123",
      rules: [{ required: true, message: "请填写密码" }],
    },
  ];
  const loginSuccessCallback = (res: any) => {
    console.log("登录成功回调", res);
    setLocalStorage(tokenKey, res?.token);
    setToken(res?.token);
    setUserInfo(res?.user);
    setUserMenu(res?.rolePermissions?.menuList)
    navigate(`/${loaderPath}`);
  };
  return (
    <>
      <LoginForm
        loginFormConfig={loginFormConfig}
        loginApiUrl={loginApiUrl}
        loginSuccessCallback={loginSuccessCallback}
      />
    </>
  );
}
